package top.mengchaob.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @title：时间转换工具
 * @author: mengchaob
 * @date: 2021年04月27日 19:43
 * @description:
 */
public class UtilTimeFormatter {
    private static final SimpleDateFormat dateymd = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat dateym = new SimpleDateFormat("yyyy-MM");
    private static final SimpleDateFormat dateymdCN = new SimpleDateFormat("yyyy年MM月dd日");
    private static final SimpleDateFormat dateymdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat dateymdhmssss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private static final SimpleDateFormat datehms = new SimpleDateFormat("HH:mm:ss");
    private static final SimpleDateFormat dateymdhm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private static final SimpleDateFormat dateymdhmsCN = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
    private static final SimpleDateFormat dateymdhmsNum = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final SimpleDateFormat dateymdhmssssNum = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private static final SimpleDateFormat subDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    private static final SimpleDateFormat datemdhm = new SimpleDateFormat("MM-dd HH:mm");
    private static final SimpleDateFormat hm = new SimpleDateFormat("HH:mm");

    public static String getdateymdhmsSSS(long date) {
        String time = dateymdhmssss.format(new Date(date));
        return time;
    }

    public static Calendar getCalendar(long date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date);
        return cal;
    }

    public static String[] subDate(long currentDate) {
        subDate.setTimeZone(TimeZone.getTimeZone("Etc/Greenwich"));
        String time = subDate.format(new Date(currentDate));
        // System.out.println(time);
        String[] sub = time.split("-");
        return sub;
    }

    public static String getdateymdhmsNum(long date) {
        String time = dateymdhmsNum.format(new Date(date));
        return time;
    }

    public static String getDatemdhm(long date) {
        String time = datemdhm.format(new Date(date));
        return time;
    }

    public static String getdateymdhmsNum(Date date) {
        String time = dateymdhmsNum.format(date);
        return time;
    }

    public static String getdateymNum(String date){
        java.util.Date dateStr;
        try {
            dateStr = dateymd.parse(date);
            String time = dateym.format(dateStr);
            return time;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;


    }

    public static String getdateymdhmsSSSNum(Date date) {
        String time = dateymdhmssssNum.format(date);
        return time;
    }

    public static String getTimeToYMD(long date) {
        String time = dateymd.format(new Date(date));
        return time;
    }

    public static String getTimeToYMD(Date date) {
        if (date == null)
            return "";
        String time = dateymd.format(date);
        return time;
    }

    public static String getTimeToYMD(String date) {
        if (date == null)
            return "";
        long ltime = 0;
        try {
            ltime = dateymd.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return getTimeToYMD(ltime);
    }

    public static String getTimeToYMDHMS(long date) {
        String time = dateymdhms.format(new Date(date));
        return time;
    }

    public static String getTimeToYMDHM(long date) {
        String time = dateymdhm.format(new Date(date));
        return time;
    }

    public static Timestamp getTimestampToYMDHMS(long date) {
        String time = dateymdhms.format(new Date(date));
        return convertDateYMDHMS(time);
    }

    public static Date getDateToYMDHMS(long date) {
        return new Date(date);
    }

    public static String getTimeToYMDHMS(Date date) {
        String time = dateymdhms.format(date);
        return time;
    }

    public static String getTimeToYMDHMS(String date) {
        String time = dateymdhms.format(date);
        return time;
    }

    public static String getTimeToYMDHMSCN(long date) {
        String time = dateymdhmsCN.format(new Date(date));
        return time;
    }

    public static String getTimeToYMDHMSCN(Date date) {
        String time = dateymdhmsCN.format(date);
        return time;
    }

    public static String getTimeToYMDHM(Date date) {
        String time = dateymdhm.format(date);
        return time;
    }

    public static String getTimeToYMDCN(long date) {
        String time = dateymdCN.format(new Date(date));
        return time;
    }

    public static String getTimeToYMDCN(Date date) {
        String time = dateymdCN.format(date);
        return time;
    }

    public static String getStringTimeYMDHMS(String time) {
        try {
            if (time.equals("null"))
                return "";
            java.util.Date date = dateymdhms.parse(time);
            String time1 = dateymdhms.format(date);
            return time1;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getStringTimeYMD(String time) {
        try {
            if (time.equals("null") || time == null)
                return "";
            java.util.Date date = dateymd.parse(time);
            String time1 = dateymd.format(date);
            return time1;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Timestamp convertDateYMD(String adateStrteStr) {
        java.util.Date date = null;
        try {
            date = dateymd.parse(adateStrteStr);
            Timestamp d = new Timestamp(date.getTime());
            return d;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Timestamp convertDateYMDHMS(String adateStrteStr) {
        java.util.Date date = null;
        try {
            date = dateymdhms.parse(adateStrteStr);
            Timestamp d = new Timestamp(date.getTime());
            return d;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static long getlongDateYMDHMS(String dateStr) {
        java.util.Date date = null;
        if (dateStr == null || dateStr.equals("")) {
            return 0;
        }
        try {
            date = dateymdhms.parse(dateStr);
            return date.getTime();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return 0;
    }

    public static String timestampToStringYMDHMS(Timestamp time) {
        return dateymdhms.format(time);
    }

    public static String timestampToStringHMS(Timestamp time) {
        return datehms.format(time);
    }

    public static String timestampToStringYMDHM(Timestamp time) {
        return dateymdhm.format(time);
    }

    public static String dateAddHour(String time) {
        try {
            java.util.Date date = dateymdhms.parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int hour = cal.get(Calendar.HOUR_OF_DAY);
            cal.set(Calendar.HOUR_OF_DAY, hour + 8);
            date = cal.getTime();
            String time1 = dateymdhms.format(date);
            return time1;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int getCurrGMT() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT-8:00"));
        int time = (int) (cal.getTime().getTime() / 1000);
        return time;
    }

    public static boolean ymdhmsCompareTo(String timeOne, String timeTwo) {
        Calendar calone = Calendar.getInstance();
        Calendar caltwo = Calendar.getInstance();
        try {
            calone.setTime(dateymdhms.parse(timeOne));
            caltwo.setTime(dateymdhms.parse(timeTwo));
            if (calone.compareTo(caltwo) == 0) {
                return true;
            }
            return false;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

    }

    public static boolean ymdhmsCompareToOne_LessORequal_Two(String timeOne, String timeTwo) {
        Calendar calone = Calendar.getInstance();
        Calendar caltwo = Calendar.getInstance();
        try {
            calone.setTime(dateymdhms.parse(timeOne));
            caltwo.setTime(dateymdhms.parse(timeTwo));
            if (calone.compareTo(caltwo) <= 0) {
                return true;
            }
            return false;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

    }

    public static boolean compareTo(String timeOne, String timeTwo) {
        Calendar calone = Calendar.getInstance();
        Calendar caltwo = Calendar.getInstance();
        try {
            calone.setTime(dateymd.parse(timeOne));
            caltwo.setTime(dateymd.parse(timeTwo));
            if (calone.compareTo(caltwo) == 0) {
                return true;
            }
            return false;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

    }

    public static boolean compareToOne_LessORequal_Two(String timeOne, String timeTwo) {
        Calendar calone = Calendar.getInstance();
        Calendar caltwo = Calendar.getInstance();
        try {
            calone.setTime(dateymd.parse(timeOne));
            caltwo.setTime(dateymd.parse(timeTwo));
            if (calone.compareTo(caltwo) <= 0) {
                return true;
            }
            return false;
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

    }

    public static void main(String[] args) {
        UtilTimeFormatter.subDate(System.currentTimeMillis());

        System.out.println(isTimeRange(System.currentTimeMillis(), "22:15", "23:59"));
    }

    // 获取当前时间加N天的方法
    public static Date afterNDay(int n) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, n);
        Date d2 = c.getTime();
        return d2;
    }

    public static Date afterNDay(int n, Date dt) {
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        c.add(Calendar.DATE, n);
        Date d2 = c.getTime();
        return d2;
    }

    public static Date afterNMinute(int n, Date dt) {
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        c.add(Calendar.MINUTE, n);
        Date d2 = c.getTime();
        return d2;
    }

    public static String formatterDayHourMin(Date startDate, Date endDate) {
        long l = endDate.getTime() - startDate.getTime();
        long day = l / (24 * 60 * 60 * 1000);
        long hour = (l / (60 * 60 * 1000) - day * 24);
        long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        return "" + day + "天" + hour + "小时" + min + "分";
    }

    public static Date StrToDate(String str) {
        return StrToDate(str,dateymd);
    }

    public static Date StrToDateYMDHMS(String str) {
        return StrToDate(str,dateymdhms);
    }

    public static Date StrToDate(String str,SimpleDateFormat formatter) {
        Date date = null;
        try {
            date = formatter.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 字符串转日期
     * @param str
     * @param pattern 格式化类型 eg:yyyy-MM-dd
     * @return
     */
    public static Date StrToDate(String str,String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = formatter.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static boolean isTimeRange(long currTime, String startTime, String endTime) {
        try {
            Date sdate = hm.parse(startTime);
            Date edate = hm.parse(endTime);
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(currTime);
            String time = hm.format(cal.getTime());
            long curr = hm.parse(time).getTime();
            if (curr >= sdate.getTime() && curr <= edate.getTime()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String dateToStringHM(Date date) {
        return hm.format(date);
    }

    public static long getlongDateHM(String dateStr) {
        java.util.Date date = null;
        if (dateStr == null || dateStr.equals("")) {
            return 0;
        }
        try {
            date = hm.parse(dateStr);
            return date.getTime();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取时间段内周几的日期
     *
     * @param from
     *            yyyy-MM-dd
     * @param to
     *            yyyy-MM-dd
     * @param weekDate
     *            Calendar.MONDAY
     * @return
     */
    public static List<String> getWeekDays(String from, String to, int weekDate) {
        Calendar calendar = Calendar.getInstance();
        String[] array = { from, to };
        Date[] ds = new Date[array.length];
        for (int i = 0; i < array.length; i++) {
            String[] fs = array[i].split("-");
            calendar.set(Integer.parseInt(fs[0]), Integer.parseInt(fs[1]) - 1, Integer.parseInt(fs[2]));
            ds[i] = calendar.getTime();
        }
        List<String> list = new ArrayList<String>();
        for (Date x = ds[0]; x.compareTo(ds[1]) <= 0;) {
            calendar.setTime(x);
            // x = calendar.getTime();
            // Calendar.MONDAY
            if (calendar.get(Calendar.DAY_OF_WEEK) == weekDate) {
                String monday = new SimpleDateFormat("yyyy-MM-dd").format(x);
                System.out.println(monday);
                list.add(monday);
            }
            calendar.add(Calendar.DATE, 1);
            x = calendar.getTime();
        }
        return list;
    }

    /**
     * @author cuijinlong
     * @param date 为完成时间
     * @param date1 分配时间
     * @param date2//充电时间
     * @param min//倒计时时间
     * @return
     */
    public static Long seconds(Date date, Date date1, BigDecimal date2, Integer min) {
        // 判断是否超时1分钟以内
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String st1 = timestampToStringHMS(Timestamp.valueOf(sdf.format(date1)));// 分配时间
        String st2 = timestampToStringHMS(Timestamp.valueOf(sdf.format(date)));// 完成时间
        String[] dates = st1.split(":");
        String[] dates1 = st2.split(":");
        long seconds = 0L;
        long seconds1 = 0L;
        long seconds2 = 0L;
        if (dates.length == 3) {
            seconds += Long.valueOf(dates[0]) * 60 * 60;
            seconds += Long.valueOf(dates[1]) * 60;
            seconds += Long.valueOf(dates[2]);
        }
        if (dates1.length == 3) {
            seconds1 += Long.valueOf(dates1[0]) * 60 * 60;
            seconds1 += Long.valueOf(dates1[1]) * 60;
            seconds1 += Long.valueOf(dates1[2]);
        }
        seconds2 = seconds1 - seconds;
        Long daojishi = Long.parseLong(min.toString()) * 60;
        Long chongdianshijian = 0L;
        if (date2 != null && date2.toString().equals("0.5")) {
            chongdianshijian = 1800L;
        } else if (date2 != null && date2.toString().equals("1")) {
            chongdianshijian = 3600L;
        } else if (date2 != null && date2.toString().equals("1.5")) {
            chongdianshijian = 5400L;
        } else if (date2 != null && date2.toString().equals("2")) {
            chongdianshijian = 7200L;
        }
        if (date2 == null) {// 非充电
            if (seconds2 > daojishi) {
                return seconds2 - daojishi;
            } else {
                return null;
            }
        } else {
            if ((seconds2 - chongdianshijian) > daojishi) {
                return seconds2 - chongdianshijian - daojishi;
            } else {
                return null;
            }

        }
    }

    public static Long minutes(Date date, Date date1, Long date2) {
        long time = date.getTime() - date1.getTime();

        time = time / (1000 * 60);

        return time = time + date2;
        // // 判断是否超时1分钟以内
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // String st1 =
        // timestampToStringHMS(Timestamp.valueOf(sdf.format(date1)));// 分配时间
        // String st2 =
        // timestampToStringHMS(Timestamp.valueOf(sdf.format(date)));// 完成时间
        // String[] dates = st1.split(":");
        // String[] dates1 = st2.split(":");
        // long seconds = 0L;
        // long seconds1 = 0L;
        // long seconds2 = 0L;
        // if (dates.length == 3) {
        // seconds += Long.valueOf(dates[0]) * 60 * 60;
        // seconds += Long.valueOf(dates[1]) * 60;
        // seconds += Long.valueOf(dates[2]);
        // }
        // if (dates1.length == 3) {
        // seconds1 += Long.valueOf(dates1[0]) * 60 * 60;
        // seconds1 += Long.valueOf(dates1[1]) * 60;
        // seconds1 += Long.valueOf(dates1[2]);
        // }
        // seconds2 = seconds1 - seconds;
        // Long chongdianshijian = date2 * 60;
        //
        // return (seconds2 + chongdianshijian) / 60;
    }
}